package com.hsj.goldstore.base;

/**
 * Created by Administrator on 2018/3/21.
 * mvpView 的绑定
 */

public class BaseMvpPresent<V extends BaseMvpView> implements IPresenter<V> {

    protected V mvpView;

    /**
     * @param mvpView 绑定view
     */
    @Override
    public void attachView(V mvpView) {
        this.mvpView = mvpView;
    }

    /**
     * view 解绑
     */
    @Override
    public void dettachView() {
        mvpView = null;
    }

    /**
     * @return true 表示已绑定view ,false表示未绑定
     */
    @Override
    public boolean isAttachView() {
        return mvpView != null;
    }

    /**
     * @return 放回绑定的view
     */
    public V getMvpView() {
        return mvpView;
    }

    /**
     * 使用present 时先检查是否绑定view
     */
    protected void checkViewAttach() {
        if (!isAttachView()) {
            throw new MvpviewNoAttachToPresentException();
        }
    }

    public static class MvpviewNoAttachToPresentException extends RuntimeException {
        public MvpviewNoAttachToPresentException() {
            super("调用presenter方法前，先调用attachView(view)方法！");
        }
    }

}
